{include file="common/header" /}
<div class="admin-mains">
    <div class="admin-main">
        <div class="top-tabs">
            <div class="list">
                <a class="active" href="{:url(CONTROLLER_NAME.'/add')}">添加{$mod.title}</a>
            </div>
            <i class="borderline"></i>
        </div>
        <div class="admin-setting layui-form pad40">
            <form id="form" action="" method="post"> 

                {include file="common/dyform"}
                
                <div class=" mt30">
                    <a class="submit-btn" style="display: inline-block;" href="javascript:;" lay-submit lay-filter="saveBtn" >提交</a>

                    <button type="button" class="layui-btn layui-btn-primary layui-border goback" style="display: inline-block;">返回</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>

    layui.use(['form','iconPickerFa','upload','laydate'], function () {
        var form = layui.form,
            iconPickerFa = layui.iconPickerFa,
            layer = layui.layer,
            upload = layui.upload,
            laydate = layui.laydate,
            $ = layui.$;

        laydate.render({
            elem: '#ID-laydate-demo'
        });

        window.imgdelete = function(dom){
            $(dom).parent().remove();
        }
        $(".goback").click(function(){
            window.location.href = "{:url(CONTROLLER_NAME.'/list')}";
        });

        form.on('select(changeShen)', function (data) {
             //获取当前选中下拉项的 value值
            var id = data.value;
            $.ajax({
                url:"{:url('Region/getRegionList')}",
                data:{'parent_id':id,'level':2},
                type:'post',
                dataType:'json',
                success:function(res){
                    console.log(res)
                    let html = '<option value="">请选择市</option>';
                    let data = res.data;
                    for(let i=0;i<data.length;i++){
                        html += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
                    }
                    $("#city").html(html);
                    $("#area").html('<option value="">请选择区</option>');
                    form.render();
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
        });

        
        form.on('select(changeCity)', function (data) {
             //获取当前选中下拉项的 value值
            var id = data.value;
            $.ajax({
                url:"{:url('Region/getRegionList')}",
                data:{'parent_id':id,'level':3},
                type:'post',
                dataType:'json',
                success:function(res){
                    console.log(res)
                    let html = '<option value="">请选择区</option>';
                    let data = res.data;
                    for(let i=0;i<data.length;i++){
                        html += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
                    }
                    $("#area").html(html);
                    form.render();
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
        });

        upload.render({
            elem: '#image_up'
            ,url: "{:url('Index/dyupload')}" 
            // ,multiple: true
            //,accept: 'file' //普通文件
            ,field: 'dypic'
            ,before: function(obj){
            }
            ,done: function(res){
              if(res.errcode){
                var html = '';
                html +='<div class="img-item">';
                if(res.count == 'file'){
                    html +='    <a href="'+ res.data +'" target="_blank"><img data-url="'+res.data+'" src="/public/static/images/pdf.png" alt=""></a>';
                }else{
                    html +='    <a href="'+ res.data +'" target="_blank"><img data-url="'+res.data+'" src="'+ res.data +'" alt=""></a>';
                }
                html +='    <i  class="layui-icon layui-icon-close-fill" onclick="imgdelete(this)"></i>';
                html +='</div>';
                
                $('#image_show').html(html)
              }
            }
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.load(1, {
                shade: [0.3,'#000'] //0.1透明度的白色背景
            });
            var data = data.field;

            // var back_new_car_image=$("#image_show .img-item img").map(function(){
            //         return ( $(this).attr("data-url") );
            //     }).get();
            //     data.head_pic = back_new_car_image;
            $.ajax({
                url:"{:url(CONTROLLER_NAME.'/add')}",
                data:data,
                type:'post',
                dataType:'json',
                success:function(res){
                    // layer.close(index);
                    // console.log(res)
                    // return false
                    if(res.errcode != 1){
                        layer.msg(res.message,{icon: 2,time: 2000},function(){
                            layer.close(index);
                        })
                    }else{
                        layer.msg(res.message,{icon: 1,time: 1000},function () {
                            window.location.href = "{:url(CONTROLLER_NAME.'/list')}";
                        })
                    }
                    layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });

            return false;
        });

    });
</script>
</body>
</html>